<!--
 * @Descripttion: 
 * @version: 
 * @Author: lyw
 * @Date: 2022-06-27 16:13:21
 * @LastEditors: fengweicong 75202172+fengweicong@users.noreply.github.com
 * @LastEditTime: 2022-07-27 16:15:00
-->
<template>
  <div class="refund">
    <div class="header" style="margin-bottom: 20px">
      <el-pagination
        background
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[1, 5, 10, 20]"
        :page-size="nums"
        layout="prev, pager, next"
        :total="total"
        style="display: inline-block"
      ></el-pagination>
      <el-button
        style="flaot: left"
        type="success"
        size="small"
        @click="dialog = true"
        round
        >新增</el-button
      >
    </div>
    <div class="main_table">
      <el-table :header-cell-style="{ background: '#f5f7fa' }" :data="list">
        <el-table-column prop="supplier_name" label="店铺名"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="toshopdetail(scope.row.id)"
              >查看店铺</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-dialog :visible.sync="dialog" center width="26%" title="店铺名">
        <el-form>
          <el-form-item class="add-box">
            <el-input
              class="input"
              v-model="shop.supplier_name"
              placeholder="请输入店铺名"
              maxlength="10"
              show-word-limit
            >
            </el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="addshop">确定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      nums: 25,
      currentPage: 1, // 当前页码
      total: 0,
      dialog: false,
      shop: {
        supplier_name: "",
        // place: "",
        // manager: "",
      }, //新增店铺信息
      list: [
        // {
        //   supplier_name: "店铺1",
        //   place: "产地1",
        //   manager: "负责人1",
        //   registerTime: "2020-06-27 16:13:21",
        // },
      ],
    };
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val;
    },
    async addshop() {
      this.shop.registerTime = new Date().toLocaleString();
      // if (this.shop.supplier_name && this.shop.place && this.shop.manager) {
      if (this.shop.supplier_name) {
        let aid = this.$store.state.user.account_id
        if (aid) {
          await this.$API
            .addSupplier({
              account_id: aid,
              supplier_name: this.shop.supplier_name,
              introduction: "",
              address: null,
              link_man: "",
              link_phone: "",
              message: "",
            })
            .then((response) => {
              if (response.status === 201) {
                this.$message.success("注册店铺成功");
                this.list.push({
                  ...this.shop,
                  id: response.data.id
                });
                this.dialog = false;
              }
            });
        } else {
          alert("没登录");
          this.dialog = false;
          return;
        }
      } else {
        this.$message.error("请填写完整信息");
      }
    },
    toshopdetail(id) {
      this.$store.dispatch("supplier/setSupplierId", id);
      // this.$router.push({
      //   name: "/saler",
      //   params: {
      //     sid: id, //店铺id
      //   },
      // });
      this.$router.push({
        name: "report",
        params: {
          sid: id, //店铺id
        },
      });
    },
    async load() {
      await this.$API.getSupplier().then((response) => {
        let aid = this.$store.state.user.account_id
        if (response.status == 200 && aid) {
          for (let obj of response.data) {
            if (obj.account_id == aid) this.list.push(obj);
          }
        }
      });
    },
  },
  created() {
    this.load();
  },
};
</script>
<style lang="less" scoped>
/deep/ .el-dialog__body{
  padding-bottom: 10px;
}
.add-box{
  margin: 0;
  .input{
    width: 100%;
  }
}
</style>